//
//  WDPlusButtonSubclass.m
//  winterTest
//
//  Created by wd on 15/10/27.
//  Copyright © 2015年 wd ,Ltd. All rights reserved.
//

#import "WDPlusButtonSubclass.h"

@interface WDPlusButtonSubclass ()<UIActionSheetDelegate>
{
    CGFloat _buttonImageHeight;
}
@end

@implementation WDPlusButtonSubclass

#pragma mark -
#pragma mark - Life Cycle

+ (void)load
{
    [super registerSubclass];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.adjustsImageWhenHighlighted = NO;
    }
    return self;
}

// 上下结构button
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 控件大小，间距大小
    CGFloat const imageViewEdge     = self.bounds.size.width * 0.6;
    CGFloat const centerOfView      = self.bounds.size.width * 0.5;
    CGFloat const labelLineHeight   = self.titleLabel.font.lineHeight;
    CGFloat const verticalMarginT    = self.bounds.size.height - labelLineHeight - imageViewEdge;
    CGFloat const verticalMargin     = verticalMarginT / 2;
    
    // imageView 和 titleLabel 中心的 Y 值
    CGFloat const centerOfImageView  = verticalMargin + imageViewEdge * 0.5;
    CGFloat const centerOfTitleLabel = imageViewEdge + verticalMargin * 2 + labelLineHeight + labelLineHeight * 0.5;
    // imageView position 位置
    self.imageView.bounds = CGRectMake(0, 0, imageViewEdge, imageViewEdge);
    self.imageView.center = CGPointMake(centerOfView, centerOfImageView);
    // title position 位置
    self.titleLabel.bounds = CGRectMake(0, 0, self.bounds.size.width, labelLineHeight);
    self.titleLabel.center = CGPointMake(centerOfView, centerOfTitleLabel);
}

#pragma mark -
#pragma mark - public methods WDPlusButtonSubclassing

+ (instancetype)plusButton
{
    WDPlusButtonSubclass *button = [[WDPlusButtonSubclass alloc]init];
    button.backgroundColor = [UIColor redColor];
    
    [button setImage:[UIImage imageNamed:@"tabbar_icon_livearea28x28"] forState:UIControlStateNormal];
    [button setTitle:@"123" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:9.5];
    [button sizeToFit];
    
    [button addTarget:button action:@selector(clickPublish) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

+ (NSUInteger)indexOfPlusButtonInTabBar
{
    return 2;
}

+ (CGFloat)multiplerInCenterY
{
    return 0.3;
}

#pragma mark -
#pragma mark - clickPublish 

- (void)clickPublish
{
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UIViewController *viewController = tabBarController.selectedViewController;
    
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil
                                                            delegate:nil
                                                   cancelButtonTitle:@"取消"
                                              destructiveButtonTitle:nil
                                                   otherButtonTitles:@"拍照", @"从相册选取", @"一键分享", @"从Safari打开", nil];
    [actionSheet showInView:viewController.view];
}

#pragma mark -
#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    NSLog(@"actionSheet index = %ld",buttonIndex);
}

@end
